<?php
class eBayToken extends errorHandler{
	public $resp;  // This is the entire response as a Simple XML object
	public $token;
	public $expiration;
	public $xml;

	function __construct($sid, $username=NULL){
		parent::__construct();

		$requestXml='<?xml version="1.0" encoding="utf-8" ?>';
		$requestXml.='<FetchTokenRequest xmlns="urn:ebay:apis:eBLBaseComponents">';
		if($username!==NULL){
			$requestXml.='	<RequesterCredentials>';
			$requestXml.='		<Username>'.$username.'</Username>';
			$requestXml.='	</RequesterCredentials>';
			$requestXml.="	<SecretID>$sid</SecretID>";
		}else{
			$requestXml.="	<SessionID>$sid</SessionID>";
		}
		$requestXml.='</FetchTokenRequest>';

		$req=new eBayRequest("FetchToken");
		if($this->xml=$req->fetch($requestXml)){
			$this->token = (string)$this->xml->eBayAuthToken;
			$this->expiration = (string)$this->xml->HardExpirationTime;
		}else{
			$this->errors=array_merge($this->errors,$req->errors);
		}
	}

	public function getToken(){
		return(($this->token && strlen($this->token)>0)?$this->token:false);
	}
}
?>